package lvtu.opencv.util;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qunar.upd.app.client.QupdClient;
import com.qunar.upd.app.client.QupdClient.ResizeMode;

/**
 * 
 * @author juan.wang
 * 
 */
public class PicUtil {
	private static final Logger log = LoggerFactory.getLogger(PicUtil.class);

	
	public static final Integer  MAX_FILE= 5242880; // 上传文件大小
	/**
	 * 压缩形式获得缩略图链接
	 * 
	 * @param picUrl
	 * @param size
	 * @return
	 */
	public static String getMiniUrl(String picUrl, String size) {
		if (StringUtils.isEmpty(picUrl))
			return null;
		if (picUrl.startsWith(UploadedPic.HOTS_URL))
			picUrl = picUrl.replaceFirst(UploadedPic.HOTS_URL, "");
		else if (picUrl.startsWith(UploadedPic.HOTS_URL_CDN))
			picUrl = picUrl.replaceFirst(UploadedPic.HOTS_URL_CDN, "");
		else if(picUrl.startsWith(UploadedPic.HOTS_URL_HEADSHOT))
			picUrl = picUrl.replaceFirst(UploadedPic.HOTS_URL_HEADSHOT, "");
		return UploadedPic.HOTS_URL_CDN
				+ QupdClient.genImgURI(picUrl, size, ResizeMode.RESIZE);
	}

	/**
	 * 压缩形式获得头像缩略图链接
	 * 
	 * @param picUrl
	 * @param size
	 * @param headsize ,默认头像大小
	 * @return
	 */
	public static String getHeadUrl(String picUrl, String size, String headsize) {
		if (StringUtils.isEmpty(picUrl))
			return null;
		String first ="";
		 if(picUrl.startsWith(UploadedPic.HOTS_URL_HEADSHOT)){
			 int index = picUrl.indexOf("?");
			 if(index != -1){
				 first = picUrl.split("[?]")[0];
			 }
			 return 	first+"?"+size;
		 }else if(picUrl.startsWith(UploadedPic.HOTS_URL_CDN) || picUrl.startsWith(UploadedPic.HOTS_URL)){
			 return getMiniUrlByCrop(picUrl,headsize);
		 }else{
			 return picUrl;
		 }
	}
	/**
	 * 控制最小边不超过某一尺寸等比压缩形式获得缩略图链接
	 * 
	 * @param picUrl
	 *            原图片路径
	 * @param width
	 *            原图片宽度
	 * @param length
	 *            原图片高度
	 * @param size
	 *            最小边尺寸不超过的尺寸
	 * @return
	 */
	public static String getResizedMiniUrlByMinEdge(String picUrl,
			Integer width, Integer length, int size) {
		if (StringUtils.isEmpty(picUrl))
			return null;
		if (size == 0)
			return picUrl;
		if (picUrl.startsWith(UploadedPic.HOTS_URL))
			picUrl = picUrl.replaceFirst(UploadedPic.HOTS_URL, "");
		else if (picUrl.startsWith(UploadedPic.HOTS_URL_CDN))
			picUrl = picUrl.replaceFirst(UploadedPic.HOTS_URL_CDN, "");
		String resize = null;
		if (width == null || width == 0 || length == null || length == 0) {
			resize = size + "x" + size;
		} else if (width < length)
			resize = size + "x5000";
		else
			resize = "5000x" + size;
		return UploadedPic.HOTS_URL_CDN
				+ QupdClient.genImgURI(picUrl, resize, ResizeMode.RESIZE);
	}

	/**
	 * 
	 * @Title: getPicCDNUrl
	 * @Description: 获取图片的CDN地址
	 * @param @param picUrl
	 * @param @return
	 * @author xiaohui.song
	 * @return String 返回类型
	 * @throws
	 */
	public static String getPicCDNUrl(String picUrl) {
		if (StringUtils.isEmpty(picUrl))
			return null;
		if (picUrl.startsWith(UploadedPic.HOTS_URL)) {
			picUrl = picUrl.replaceFirst(UploadedPic.HOTS_URL, "");
			return UploadedPic.HOTS_URL_CDN + picUrl;
		} else {
			return picUrl;
		}
	}

	/**
	 * 剪裁形式获得缩略图链接
	 * 
	 * @param picUrl
	 * @param size
	 * @return
	 */
	public static String getMiniUrlByCrop(String picUrl, String size) {
		if (StringUtils.isEmpty(picUrl))
			return null;
		if (picUrl.startsWith(UploadedPic.HOTS_URL))
			picUrl = picUrl.replaceFirst(UploadedPic.HOTS_URL, "");
		else if (picUrl.startsWith(UploadedPic.HOTS_URL_CDN))
			picUrl = picUrl.replaceFirst(UploadedPic.HOTS_URL_CDN, "");
		return UploadedPic.HOTS_URL_CDN
				+ QupdClient.genImgURI(picUrl, size, ResizeMode.CROP);
	}



}
